<template>
	<view class="content">
		<view class="tibvolo">
			<image @click="fanhiskan" class="dingbudefanhui"
				src="https://dfgq3654.laidianbanlv.cn/images/duanju/baisefanhui.png" mode=""></image>
		</view>
		


		<view class="shourubox">
			<view class="yanzhengdeng">
				验证码登录
			</view>
			<view class="shoujihabox">
				<!-- <view class="shjoujiha">
					手机号
				</view> -->
				<view class="shjoujinput">
					<input v-model="phonenum" type="number" maxlength="11" placeholder="请输入手机号" />
				</view>
			</view>
			<view class="shoujihabox">
				<!-- <view class="shjoujiha">
					验证码
				</view> -->
				<view class="shuyanzhengma">
					<input type="number" v-model="yzcode" maxlength="11" placeholder="请输入验证码" />

					<view class="shjoujyanzhen">
						<u-toast ref="uToast"></u-toast>
						<u-verification-code :seconds="seconds" @end="end" @start="start" ref="uCode"
							@change="codeChange"></u-verification-code>
						<u-button @tap="getCode">{{tips}}</u-button>
					</view>
				</view>


			</view>
		</view>


		<view @click="uselogon" :class="yzcode?'kuiaidnewg':'notkuiaidnewg'">
			登录
		</view>
		<!-- <button :class="usersele?'kuiaidnewg':'notkuiaidnewg'" open-type="getPhoneNumber"
			@getphonenumber="getPhoneNumber">快捷登录</button> -->

	</view>
</template>

<script>
	import request from '../../utils/request.js';
	import api from '../../utils/url.js';
	import {
		mapState,
		mapActions
	} from 'vuex'
	export default {
		data() {
			return {
				usersele: false,
				// token: "",
				tips: '',
				seconds: "60",
				phonenum: "",
				yzcode: "",
				duanxinkey: "",
			}
		},
		computed: {
			...mapState({
				token: state => state.token,
			})
		},
		onLoad() {
			// console.log(mapState)
			// console.log(mapState)
		},
		methods: {
			uselogon() {
				var that = this
				let data = {
					mobile: this.phonenum,
					code: String(this.yzcode),
					key: this.duanxinkey,
				}
				request(api.mobile_login, 'POST', data)
					.then(response => {
						console.log('mobile_login数据:', response.data);
						// console.log('token数据:', response.data.token);
						if (response.code == "200") {

							localStorage.setItem("token", response.data.token);

							that.$store.commit('seTtoken', response.data.token);
							uni.showToast({
								title: "登录成功",
								icon: "none"
							})

							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							}, 1500)

						} else {
							uni.showToast({
								title: response.msg,
								icon: "none"
							})
						}
					})
					.catch(response => {
						uni.hideLoading()
					})
			},
			codeChange(text) {
				this.tips = text;
			},
			getCode() {
				if (this.$refs.uCode.canGetCode) {
					// 模拟向后端请求验证码
					uni.showLoading({
						title: '正在获取验证码'
					})
					let data = {
						mobile: this.phonenum,
					}
					request(api.send_code, 'POST', data)
						.then(response => {
							console.log('areaarea数据:', response.data);
							if (response.code == "200") {
								this.duanxinkey = response.data.key
								this.seconds = response.data.countdown
								//测试用
								if (response.data.code) {
									this.yzcode = response.data.code
								}
								uni.hideLoading();
								// 这里此提示会被this.start()方法中的提示覆盖
								this.$u.toast('验证码已发送');
								// 通知验证码组件内部开始倒计时
								this.$refs.uCode.start();
							} else {
								uni.showToast({
									title: response.msg,
									icon: "none"
								})
							}
						})
						.catch(response => {
							uni.hideLoading()
						})
				} else {
					this.$u.toast('倒计时结束后再发送');
				}


			},
			end() {
				// this.$u.toast('倒计时结束');
			},
			start() {
				// this.$u.toast('倒计时开始');
			},
			useclicksele() {
				this.usersele = !this.usersele
			},
			fanhiskan(){
				uni.navigateBack({
					delta: 1
				})
			},
			getPhoneNumber(res) {
				var that = this
				if (!this.usersele) {
					uni.showToast({
						title: '请勾选用户协议',
						icon: 'none',
					})
					return
				}
				console.log(res, '手机号')
				console.log(res.detail.code)
				let data = {
					phone_code: res.detail.code,
					token: this.token,
				}
				request(api.mini_bind_mobile, 'POST', data, this.token)
					.then(res => {
						console.log('getPhoneNumber接口:', res);
						if (res.error_code == 0) {
							uni.setStorageSync('token', that.token);
							uni.showToast({
								title: '登录成功',
								icon: 'none',
							})
							setTimeout(() => {
								uni.navigateBack({
									delta: 1
								})
							})
						} else {
							uni.showToast({
								title: res.msg,
								icon: 'none',
							})
						}
					})
			},



		}
	}
</script>

<style lang="scss">
	// page{
	// 	background-color: #f8f8f8;
	// }
	page {
		background-color: #f8f8f8;
	}

	.shourubox {
		width: 90%;
		margin-top: -50px;
		margin-left: 5%;
		// border: 1rpx solid #bababa;
		border-radius: 20rpx;
		padding: 40rpx 0 60rpx 0;
		background-color: #fff;
	}

	.shoujihabox {
		display: flex;
		flex-flow: row;
		justify-content: left;
		align-items: center;
		margin-top: 30rpx;
	}

	.shjoujinput input {
		border: 1rpx solid #bababa;
		margin-left: 30rpx;
		width: 580rpx;
		height: 80rpx;
		border-radius: 20rpx;
		padding: 0 20rpx;
	}

	.shuyanzhengma {
		border: 1rpx solid #bababa;
		margin-left: 30rpx;
		width: 620rpx;
		height: 80rpx;
		border-radius: 20rpx;
		padding: 0 20rpx;

		display: flex;
		flex-flow: row;
		justify-content: space-between;
		align-items: center;
	}

	// .shuyanzhengma input{
	// 	line-height: ;
	// }

	.shjoujyanzhen {
		width: 180rpx;
		border-radius: 20rpx;
		margin-left: 60rpx;

	}

	.u-size-default {
		height: 60rpx;
	}

	.u-size-default {
		font-size: 28rpx;
	}

	.shjoujiha {
		font-size: 32rpx;
		margin-left: 30rpx;
	}

	.logo {
		width: 100%;
		// margin-top: 100rpx;
	}

	.logo image {
		width: 600rpx;
		height: 600rpx;
		display: block;
		margin: 0 auto;
	}

	.xieyi {
		width: 100%;
		display: flex;
		flex-flow: row;
		justify-content: center;
		align-items: center;
	}

	.xieyi image {
		width: 40rpx;
		height: 40rpx;
		margin-right: 10rpx;
	}

	.xieyi text {
		color: #ffaa00;
	}

	.kuiaidnewg {
		width: 600rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 50rpx;
		background-color: #3CB79B;
		color: white;
		display: block;
		margin: 40rpx auto;
		border: none;
		font-size: 28rpx;
		margin-top: 80rpx;
	}

	.notkuiaidnewg {
		width: 600rpx;
		height: 80rpx;
		text-align: center;
		line-height: 80rpx;
		border-radius: 50rpx;
		background-color: #cccccc;
		color: white;
		display: block;
		margin: 40rpx auto;
		border: none;
		font-size: 28rpx;
		margin-top: 80rpx;
	}

	.u-btn--default {
		background-color: #5199FE;
		color: white;
	}

	.tibvolo {
		width: 100%;
		height: 300rpx;
		border-radius: 0 0 150rpx 150rpx;
		background-color: #5199FE;
	}

	.yanzhengdeng {
		text-align: center;
		color: #3CB79B;
		font-size: 38rpx;
		font-weight: bold;
	}
	.dingbudefanhui{
		width: 40rpx;
		height: 40rpx;
		margin-left: 40rpx;
		margin-top: 40rpx;
	}
</style>